//
//  KitScrollView.m
//  Demo_ScrollView
//
//  Created by 刘虎 on 16/3/17.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "KitScrollView.h"

@interface KitScrollView ()<UIScrollViewDelegate>

@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageControl;

@end


@implementation KitScrollView

- (instancetype)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArr period:(CGFloat)period{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutSubviewsWithImagesArray:imagesArr];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        self.scrollView.delegate = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:period target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    }
    return self;
}

#pragma mark - 布局
- (void)layoutSubviewsWithImagesArray:(NSArray *)imagesArr {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width*(imagesArr.count+2), self.frame.size.height);
    for (int i = 0; i <= imagesArr.count+1; i++) {
        UIImage *image = [[UIImage alloc] init];
        if (i == 0) {
            image = [imagesArr lastObject];
        } else if (i == imagesArr.count+1) {
            image = [imagesArr firstObject];
        } else {
            image = [imagesArr objectAtIndex:i-1];
        }
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
        imageView.image = image;
        [self.scrollView addSubview:imageView];
    }
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 20)];
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = imagesArr.count;
    [self addSubview:self.pageControl];
    [self.pageControl addTarget:self action:@selector(selectedImage) forControlEvents:UIControlEventValueChanged];
    
}

#pragma mark - 时钟回调方法
- (void)nextImage {
    
    CGPoint point = self.scrollView.contentOffset;
    point.x = (int)(point.x/self.frame.size.width+1) * self.frame.size.width;
    [self.scrollView setContentOffset:point animated:YES];
    self.pageControl.currentPage = point.x/self.frame.size.width-1;
}

#pragma mark - scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger sumPage = scrollView.contentSize.width/self.frame.size.width;
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width*(sumPage-2), 0) animated:NO];
        self.pageControl.currentPage = sumPage-3;
    }
    if (scrollView.contentOffset.x == (sumPage-1)*self.frame.size.width) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.pageControl.currentPage = 0;
    }
}

#pragma mark - PageControl的回调
- (void)selectedImage {
    
    CGPoint point = self.scrollView.contentOffset;
    point.x = self.frame.size.width * (self.pageControl.currentPage+1);
    [self.scrollView setContentOffset:point animated:YES];
}





@end
